package agentsSME;

import java.util.LinkedList;

import main.BuildModel;
import main.Organization;
import otherOrganizations.ServiceCompany;
import enviornment.FloodWarning;
/**
 * Is a message sender. It sends messages to others.
 * 
 * @author Chunhui Li
 *
 */
public class MsgSender {
	private LinkedList<SME> sendTo;
	private SME SMESender;
	private ServiceCompany serviceSender;
	
	//for group messages
	public MsgSender() {
		sendTo = new LinkedList<SME>();
	}
	
	public MsgSender(SME s) {
		sendTo = new LinkedList<SME>();
		SMESender=s;
	}
	public MsgSender(ServiceCompany s) {
		sendTo = new LinkedList<SME>();		
		serviceSender=s;
	}
	public SME getSMESender(){
		return SMESender;
	}
	public ServiceCompany getServiceSender(){
		return serviceSender;
	}
	public void setSMESender(SME o){
		SMESender=o;
	}
	public void setServiceSender(ServiceCompany o){
		serviceSender=o;
	}
	/**
	 * This method is to set up the list of SME recipients. This is used by EA agent to send out flood warnings
	 * @param s a list of SME recipients
	 */
	public void setSendingList(LinkedList<SME> s){
		sendTo.addAll(s);
	}
	public void addSubscriber(SME s){
		sendTo.add(s);
	}
	public LinkedList<SME> getContactList(){
		return sendTo;
	}
	/**
	 * Sends an alert by EA agent.
	 * @param alert a flood warning
	 */
	public void sendGroupWarning(FloodWarning alert){
	
		if (!sendTo.isEmpty()){
			for (int i =0; i< sendTo.size();i++){
				SME sub= sendTo.get(i);
               // sub.getReceiver().receiveMsg(alert, sub);
				sub.getReceiver().receiveFloodMsg(alert);
			}
			
		}
		else {
			System.out.println("No contacts!!!!"); // write an exception class later
		}
		
	}
	
	/**
	 * Send messages to SMEs from service companies
	 * @param msg
	 * @param o
	 */
	public void sendMsgToSME(String msg, String o){	
		Message m=new Message(serviceSender.getCompanyName(), o, msg);
		ManufactureSME sme=searchSMEByName(o);
		sme.getReceiver().receiveMessage(m);
		System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%% SEND A MESSAGE:" +m);
	}
	/**
	 * Send messages to SMEs from SMEs
	 * @param msg
	 * @param o
	 */
	public void smeToSME(String msg, String o){	
		Message m=new Message(SMESender.getName(), o, msg);
		ManufactureSME sme=searchSMEByName(o);
		sme.getReceiver().receiveMessage(m);
		System.out.println(SMESender.getName()+" SEND A MESSAGE:" +m);
	}
/**
 * send messages to service companies from SMEs
 * @param msg
 * @param o
 */
	public void sendMsgToService(String msg, String o){	
		Message m=new Message(SMESender.getName(), o, msg);
//		String m=msg+","+SMESender.getName()+"," +o;
		ServiceCompany x=searchCompanyByName(o);
		x.getReceiver().receiveMessage(m);
				
	}
	private ManufactureSME searchSMEByName (String s){
		ManufactureSME found=new ManufactureSME();
		for(SME sme: BuildModel.smeContext){
			int i=sme.getName().compareTo(s);
			if(i==0){
				found =(ManufactureSME)sme;
			}
		}
		return found;
	}
	
	private ServiceCompany searchCompanyByName (String s){
		ServiceCompany found=new ServiceCompany(3, "");
		for(ServiceCompany x: SMESender.GetServiceCompanyList()){	
			int i= x.getCompanyName().compareTo(s);
			if(i==0){
				found =x;
			}
		}
		return found;
	}

}
